﻿
using System ;
using System.Windows.Forms ;

using MskEdit.Msk.Model ;


namespace MskEdit.Msk.Frontend
{

    public class MskTabPage : TabPage
    {
        #region Private data
        
        private MskPagePanel mskPagePanel_ ;
        
        #endregion
    
        #region Private event handlers

        private void handleLineSelectionChanged( MskLineKey mskLineKey ) {
        
            if ( LineSelectionChanged == null )
                return ;
                
            LineSelectionChanged.Invoke( mskLineKey ) ;
        }
        
        
        private void handleVisibleChanged() {        
            if ( this.Visible == false )
                mskPagePanel_.clearMskLineSelection() ;                
        }

        #endregion

        #region Private foundation

        private MskTabPage( MskPagePanel mskPagePanel, string pageKeyAsString )
        {
            mskPagePanel_ = mskPagePanel ;
            mskPagePanel_.LineSelectionChanged += this.handleLineSelectionChanged ;
            this.Controls.Add( mskPagePanel ) ;
            this.Text = pageKeyAsString ;
            
            this.VisibleChanged += (sender, args) => handleVisibleChanged() ;
        }
        
        #endregion

        #region Public event declarations

        public delegate void LineSelectionChangedEventHandler( MskLineKey mskLineKey ) ;
        public event LineSelectionChangedEventHandler LineSelectionChanged ;

        #endregion

        #region Public foundation

        public static MskTabPage From( MskPage mskPage, MskFieldGeometer fieldGeometer, bool showGrid ) {
        
            MskPagePanel mskPagePanel = MskPagePanel.From( mskPage, fieldGeometer, showGrid ) ;
            MskTabPage mskTabPage = new MskTabPage( mskPagePanel, mskPage.PageKey.ToString() ) ;
                    
            return mskTabPage ;
        }

        #endregion
        
        #region Public operations

        public void updateMskLine( MskLine mskLine ) {
            mskPagePanel_.updateMskLine( mskLine ) ;
        }
    
        #endregion        

        #region Public properties
        
        public MskPagePanel MskPagePanel {
            get { return mskPagePanel_ ; }
        }
        
        #endregion
    }
}
